package com.itboye.pondteam.pondteam;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.pondteam.R;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.custom.ClearEditText;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.EmailUtil;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.pondteam.utils.EmptyUtil.getCustomText;
import static com.itboye.pondteam.utils.EmptyUtil.isEmpty;


/**
 * Created by itboye on 2017/2/24.
 */

public class RegisterActivity extends BaseActivity implements Observer {

    ClearEditText cleEdEmail;//邮箱
    ClearEditText clearPassword;//登录密码
    ClearEditText clearUserName;//用户呢称
    ClearEditText clearAddress;//地址信息
    ClearEditText clearPhone;//联系电话
    private Button btnDetermine,btncancel;
    UserPresenter userPresenter;
    TextView txt_title;
    ImageView img_back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userPresenter=new UserPresenter(this);
        txt_title.setText(getString(R.string.newuser_register));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;

            case R.id.btnDetermine:
                if (isEmpty(cleEdEmail)) {
                    MAlert.alert(getString(R.string.email_empty));
                    return;
                }
                if (isEmpty(clearPassword)) {
                    MAlert.alert(getString(R.string.password_empty));
                    return;
                }
//                if (isEmpty(clearUserName)) {
//                    MAlert.alert(getString(R.stringWeeks.email_empty));
//                    return;
//                }
//                if (isEmpty(clearAddress)) {
//                    MAlert.alert(getString(R.stringWeeks.email_empty));
//                    return;
//                }
//                if (isEmpty(clearPhone)) {
//                    MAlert.alert(getString(R.stringWeeks.email_empty));
//                    return;
//                }

                if (EmailUtil.emailValidation(cleEdEmail.getText().toString()) != true) {
                    MAlert.alert("邮箱格式不对");
                    return;
                }
                String email=getCustomText(cleEdEmail);
                String password=getCustomText(clearPassword);
                String nickName=getCustomText(clearUserName);
                String address=getCustomText(clearAddress);
                String phone=getCustomText(clearPhone);
                userPresenter.registerByEmail(nickName,phone,address,email,password);
                break;
            case R.id.btncancel:
                finish();
                break;
        }

    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity resultEntity = handlerError(data);
        if (resultEntity != null) {
            if (resultEntity.getCode() != 0) {
                MAlert.alert(resultEntity.getMsg());
                return;
            }
            if(resultEntity.getEventType()==UserPresenter.register_success){
                MAlert.alert(resultEntity.getData()+"success");
                finish();
            }else if(resultEntity.getEventType()==UserPresenter.register_success){
                MAlert.alert(resultEntity.getData()+"fail");
            }
        }
    }
}
